package com.ef.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.ef.constant.EContractState;
import com.ef.constant.EPayMethod;
import com.ef.constant.StatusType;

@Entity
public class Contract implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@Enumerated(value = EnumType.STRING)
	private StatusType status = StatusType.VALID;

	@Column(length = 100)
	private String contractNo;

	/**
	 * 一个合同只对应一个人
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	private Leads leads;

	@Temporal(TemporalType.TIMESTAMP)
	private Date startTime;

	@Temporal(TemporalType.TIMESTAMP)
	private Date endTime;

	private Double price;

	@ManyToOne(fetch = FetchType.EAGER)
	private Course course;

	@ManyToMany(cascade = { CascadeType.ALL })
	private List<ClassUnit> classUnits;

	@ManyToMany(cascade = { CascadeType.ALL })
	private List<Gift> gifts;

	@ManyToMany(cascade = { CascadeType.ALL })
	private List<Material> materials;

	@Enumerated(value = EnumType.STRING)
	private EContractState contractState;

	@ManyToOne(fetch = FetchType.EAGER)
	private ClassLevel beginLevel;

	@Enumerated(value = EnumType.STRING)
	private EPayMethod payMethod;

	@Column(length = 100)
	private String userName;

	@Column(length = 200)
	private String card;

	@Column(length = 200)
	private String eliteCard;

	@Column(length = 1000)
	private String comments;

	private Boolean isVip = false;

	private Double paidDeposit;

	@Temporal(TemporalType.TIMESTAMP)
	private Date paidDepositTime;

	@ManyToOne(fetch = FetchType.EAGER)
	private User createUser;
	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;
	@ManyToOne(fetch = FetchType.EAGER)
	private User commitUser;
	@Temporal(TemporalType.TIMESTAMP)
	private Date commitTime;
	@Temporal(TemporalType.TIMESTAMP)
	private Date finishTime;
	@ManyToOne(fetch = FetchType.EAGER)
	private User finishUser;

	private Integer freeF2FTimes;

	private Double paidMoney;

	@ManyToOne(fetch = FetchType.LAZY)
	private User updateUser;

	@Temporal(TemporalType.TIMESTAMP)
	private Date updateTime;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getCard() {
		return card;
	}

	public void setCard(String card) {
		this.card = card;
	}

	public String getEliteCard() {
		return eliteCard;
	}

	public void setEliteCard(String eliteCard) {
		this.eliteCard = eliteCard;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public EContractState getContractState() {
		return contractState;
	}

	public void setContractState(EContractState contractState) {
		this.contractState = contractState;
	}

	public EPayMethod getPayMethod() {
		return payMethod;
	}

	public void setPayMethod(EPayMethod payMethod) {
		this.payMethod = payMethod;
	}

	public String getContractNo() {
		return contractNo;
	}

	public void setContractNo(String contractNo) {
		this.contractNo = contractNo;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Leads getLeads() {
		return leads;
	}

	public void setLeads(Leads leads) {
		this.leads = leads;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public List<ClassUnit> getClassUnits() {
		return classUnits;
	}

	public void setClassUnits(List<ClassUnit> classUnits) {
		this.classUnits = classUnits;
	}

	public List<Gift> getGifts() {
		return gifts;
	}

	public void setGifts(List<Gift> gifts) {
		this.gifts = gifts;
	}

	public List<Material> getMaterials() {
		return materials;
	}

	public void setMaterials(List<Material> materials) {
		this.materials = materials;
	}

	public ClassLevel getBeginLevel() {
		return beginLevel;
	}

	public void setBeginLevel(ClassLevel beginLevel) {
		this.beginLevel = beginLevel;
	}

	public StatusType getStatus() {
		return status;
	}

	public String getContractStateMsg() {
		if (contractState != null) {
			return contractState.toString();
		}
		return "";
	}

	public void setStatus(StatusType status) {
		this.status = status;
	}

	public Double getPaidDeposit() {
		return paidDeposit;
	}

	public void setPaidDeposit(Double paidDeposit) {
		this.paidDeposit = paidDeposit;
	}

	public Date getPaidDepositTime() {
		return paidDepositTime;
	}

	public void setPaidDepositTime(Date paidDepositTime) {
		this.paidDepositTime = paidDepositTime;
	}

	public User getCreateUser() {
		return createUser;
	}

	public void setCreateUser(User createUser) {
		this.createUser = createUser;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public User getCommitUser() {
		return commitUser;
	}

	public void setCommitUser(User commitUser) {
		this.commitUser = commitUser;
	}

	public Date getCommitTime() {
		return commitTime;
	}

	public void setCommitTime(Date commitTime) {
		this.commitTime = commitTime;
	}

	public Integer getFreeF2FTimes() {
		return freeF2FTimes;
	}

	public void setFreeF2FTimes(Integer freeF2FTimes) {
		this.freeF2FTimes = freeF2FTimes;
	}

	public String getSelLeadsInfo() {
		if (this.getLeads() == null) {
			return "";
		}
		String tel = "";
		if (this.getLeads().getMobileTelephone() != null) {
			tel = "[TEL:" + getLeads().getMobileTelephone() + "]";
		}
		return this.getLeads().getName() + tel;
	}

	public Boolean getIsVip() {
		return isVip;
	}

	public void setIsVip(Boolean isVip) {
		this.isVip = isVip;
	}

	public Double getPaidMoney() {
		return paidMoney;
	}

	public void setPaidMoney(Double paidMoney) {
		this.paidMoney = paidMoney;
	}

	public Date getFinishTime() {
		return finishTime;
	}

	public void setFinishTime(Date finishTime) {
		this.finishTime = finishTime;
	}

	public User getFinishUser() {
		return finishUser;
	}

	public void setFinishUser(User finishUser) {
		this.finishUser = finishUser;
	}

	public User getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(User updateUser) {
		this.updateUser = updateUser;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Boolean needPay() {
		if (contractState != null
				&& contractState.equals(EContractState.COMMIT)) {
			if (this.paidMoney== null||this.paidMoney <this.price-1) {
				return true;
			}
		}
		return false;
	}
}
